package com.sux.demo;

import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.util.CharsetUtil;

public class WebSocketClientHost {
    public static void main(String[] args) {
        WebSocketClient webSocketClient = new WebSocketClient();

        SendDataToServerThread thread = new SendDataToServerThread(webSocketClient);
        thread.start();

        webSocketClient.connect("127.0.0.1", 40005, "WebSocket客户端");
    }
}

class SendDataToServerThread extends Thread {
    private WebSocketClient webSocketClient;

    private int index = 1;

    public SendDataToServerThread(WebSocketClient webSocketClient) {
        this.webSocketClient = webSocketClient;
    }

    @Override
    public void run() {
        try {
            while (index <= 5000000) {
                Channel channel = webSocketClient.getChannel();
                if (channel != null && channel.isActive()) {
                    String msg = "客户端发送的测试消息, index = " + index;
                    channel.writeAndFlush(new TextWebSocketFrame(Unpooled.copiedBuffer(msg, CharsetUtil.UTF_8)));
                    index++;
                }
                Thread.sleep(10000);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
